package edu.bistu.sim.xwy.blog.Controller;

//import com.sun.org.apache.bcel.internal.generic.NEW;
import edu.bistu.sim.xwy.blog.Service.CollectInfoService;
import edu.bistu.sim.xwy.blog.Service.NewsComService;
import edu.bistu.sim.xwy.blog.Service.NewsInfoService;
import edu.bistu.sim.xwy.blog.Service.NewsUserService;
import edu.bistu.sim.xwy.blog.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/collect")
public class CollectInfoController {

    @Autowired
    private NewsUserService newsUserService;
    @Autowired
    private NewsInfoService newsInfoService;
    @Autowired
    private CollectInfoService colsComService;

    @PostMapping("/addcol")
    public CollectInfo addCom(@Param("news_id")int news_id, @Param("user_tel")String user_tel){
        CollectInfo colsInfo = new CollectInfo();
        NewsUser col_user = newsUserService.getOne(newsUserService.findUserByTel(user_tel).getUser_id());
        NewsInfo col_news = newsInfoService.findByNew_id(news_id);

        colsInfo.setCollct_news_id(col_news);
        colsInfo.setCollct_user_tel(col_user);
        List<CollectInfo> collectInfoList=new ArrayList<>();
        collectInfoList=colsComService.findAll();
        for (CollectInfo collectInfo :collectInfoList){
            if (collectInfo.getCollct_user_tel()==colsInfo.getCollct_user_tel() && collectInfo.getCollct_news_id()==colsInfo.getCollct_news_id()){
                System.out.println("数据库中已有相关记录");
                colsComService.deleteCol(col_news);
                System.out.println("News_id is "+col_news.getNews_id());
                return null;
            }
        }
        colsComService.save(colsInfo);

        return colsInfo;
    }
    @GetMapping("/AllNewsBytel")
    public List<NewsInfo> findAllNewsBytel(@Param("user_tel")String user_tel) {

        List<CollectInfo> collectInfo=colsComService.findAll();
        collectInfo=colsComService.findAll();
        List<CollectInfo> collectInfo2 = new ArrayList<CollectInfo>();
        List<NewsInfo> newsInfos=new ArrayList <NewsInfo>();
        for (CollectInfo collectInfo1 : collectInfo){

            if(collectInfo1.getCollct_user_tel().getUser_tel().equals(user_tel)){
                newsInfos.add(collectInfo1.getCollct_news_id());
            }
        }
        return newsInfos;

    }

    @GetMapping("/findAll")
    public List<CollectInfo> findAll(){
        return colsComService.findAll();
    }
    @GetMapping("/deleteCol")
    public void deleteCol(NewsInfo col_news){
        //删除收藏信息
        colsComService.deleteCol(col_news);
    }

}
